home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0104_Frequency Analyzer.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  3KB  |  152 lines

  1. {
  2. JL>  #2: Another thing, I've got this cool Lotto program where I would like to
  3.   >  a date file where the user can enter the weeks winning lotto numbers, then
  4.   >  after a collection of weeks is made (say 10), the computer will read all t
  5.   >  numbers in the file and compile a list of the most frequently ocurring num
  6.   >  and print them out to the screen. I'm having trouble reading from and writ
  7.   >  to the file. (I'll tackle the list compiling once that is straightened out
  8.   >  help?
  9.  
  10.   Oh Boy, Lotto programs, the concept is pregnant with possibilities!
  11.   Ever wonder why someone with a lotto program would sell it and not
  12.   just win all the lottos? :)
  13.  
  14.   Ok, you want a frequency analyzer. Here's a start that will let you
  15.   enter numbers and give a frequency table of all the numbers to date
  16.   (hey, this is kinda fun, maybe I'll go into the lottery seminar
  17.   bidness. Look out, Becky Paul!):
  18. }
  19.  
  20. {$i-}
  21. uses
  22.   crt;
  23.  
  24. const
  25.   MAX = 49;
  26.  
  27. type
  28.   tFreqArray= array[0..MAX] of word;
  29.  
  30. var
  31.   freqArray : tFreqArray;
  32.  
  33. {----------------------}
  34. procedure InitFreqArray;
  35. { Read data file into array. If not found, zero all accumulators. }
  36.  
  37. var
  38.   FreqF : file of tFreqArray;
  39.  
  40. begin
  41.   assign(FreqF,'lotto.dat');
  42.   reset(FreqF);
  43.   if (ioresult=0) then begin
  44.     read(Freqf,freqArray);
  45.     close(freqF);
  46.   end else fillchar(FreqArray,sizeof(FreqArray),0);
  47. end;
  48.  
  49. {----------------------}
  50. procedure SaveFreqArray;
  51. var
  52.   FreqF : file of tFreqArray;
  53.  
  54. begin
  55.   assign(FreqF,'lotto.dat');
  56.   rewrite(FreqF);
  57.   write(Freqf,freqArray);
  58.   close(freqF);
  59. end;
  60.  
  61. {----------------------}
  62. procedure PrintFrequencyTable;
  63.  
  64. type
  65.   tPickRec=record
  66.     Number : byte;
  67.     Freq : word;
  68.   end;
  69.   tPickArray=array[0..MAX] of tPickRec;
  70.  
  71. var
  72.   PickArray : tPickArray;
  73.  
  74. {-----------}
  75. procedure SortPickArray;
  76.  
  77. {-----------}
  78. procedure Swap(One,TheOther : byte);
  79. var
  80.   tmp : tPickRec;
  81.  
  82. begin
  83.   tmp:= PickArray[One];
  84.   PickArray[One]:= PickArray[TheOther];
  85.   PickArray[TheOther]:= tmp;
  86. end;
  87.  
  88. {----------}
  89. var
  90.   i,j,min : byte;
  91.  
  92. begin
  93.   for i:= 0 to pred(MAX) do begin
  94.     min:= i;
  95.     for j:= succ(i) to MAX do
  96.       if (PickArray[j].freq > PickArray[min].freq) then  min:= j;
  97.     if (min>i) then Swap(i,min);
  98.   end;
  99. end; {SortPickArray}
  100.  
  101. {--------}
  102. var
  103.   i : byte;
  104.  
  105. begin
  106.   for i:= 0 to MAX do with PickArray[i] do begin
  107.     Number:= i;
  108.     Freq:= FreqArray[i];
  109.   end;
  110.  
  111.   SortPickArray;
  112.   clrscr;
  113.   writeln;
  114.   writeln('Frequency Table:');
  115.   for i:= 0 to 9 do
  116.     writeln(PickArray[i].Number   :7,': ',PickArray[i].Freq   :5,' ',
  117.             PickArray[i+10].Number:7,': ',PickArray[i+10].Freq:5,' ',
  118.             PickArray[i+20].Number:7,': ',PickArray[i+20].Freq:5,' ',
  119.             PickArray[i+30].Number:7,': ',PickArray[i+30].Freq:5,' ',
  120.             PickArray[i+40].Number:7,': ',PickArray[i+40].Freq:5,' ');
  121.  
  122. end; {PrintFrequencyTable}
  123.  
  124. {----------------------}
  125. procedure GetLottoNumbers;
  126. var
  127.   OneNumber : byte;
  128.   Test : integer;
  129.   s : string;
  130.  
  131. begin
  132.   PrintFrequencyTable;
  133.   repeat
  134.     writeln;
  135.     write('Enter lotto number (<=',MAX,', Enter to quit): ');
  136.     readln(s);
  137.     if (s<>'') then begin
  138.       val(s,OneNumber,test);
  139.       if (test=0) then begin
  140.         inc(FreqArray[OneNumber]);
  141.         PrintFrequencyTable;
  142.       end;
  143.     end;
  144.   until (s='');
  145.  
  146. end; {GetLottoNumbers}
  147. begin
  148.   InitFreqArray;
  149.   GetLottoNumbers;
  150.   SaveFreqArray;
  151. end.
  152.